﻿using HighThroughput.ShareData;
using OSSUtility;
using Ozone.Help;
using Ozone.Models;
using Ozone.ShareData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ozone.Views.MethodCenter
{
    /// <summary>
    /// UCWorkLog.xaml 的交互逻辑
    /// </summary>

    public partial class UCWorkLog : UserControl
    {
        List<AppLog> lstApplog = new List<AppLog>();
        public UCWorkLog()
        {
            InitializeComponent();

            RefreshLogThread();
        }

        bool bRefreshLog = false;
        //刷新日志数据线程
        private void RefreshLogThread()
        {
            bRefreshLog = true;

            Task.Run(() =>
            {
                while (bRefreshLog)
                {
                    Thread.Sleep(1000);
                    this.Dispatcher.Invoke(new Action(
                        delegate
                        {
                            try
                            {

                                Refresh();
                            }
                            catch (Exception ex)
                            {

                            }
                        }
                        )
                    );
                }
            });

        }


        private void Refresh()
        {
            bool lockTaken = false;
            try
            {
                Monitor.TryEnter(AppLogData.Entity.objAppLogLock, 1000, ref lockTaken);
                if (lockTaken)
                {
                    if (AppLogData.Entity.lstApplog.Count == 0)
                        return;

                    for (int i = AppLogData.Entity.lstApplog.Count - 1; i >= 0; i--)
                    {
                        lstApplog.Insert(0, new AppLog(AppLogData.Entity.lstApplog[i].MsgType,
                                                        AppLogData.Entity.lstApplog[i].FContent));
                    }

                    AppLogData.Entity.lstApplog.Clear();

                    dgLog.ItemsSource = null;
                    dgLog.ItemsSource = lstApplog;
                }

            }
            catch (Exception ex)
            {
               
            }
            finally
            {
                // Ensure that the lock is released.
                if (lockTaken)
                {
                    Monitor.Exit(AppLogData.Entity.objAppLogLock);
                }
            }




        }

        public void Clear()
        {
            try
            {
                lstApplog.Clear();
                dgLog.ItemsSource = null;
            }
            catch (Exception e)
            {

            }
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            Clear();
        }
    }
}
